/**
 * 
 */
package com.hanhai.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * leiruiqi@hanhai.com
 */
public class CommonSMSService {

	private static Logger logger = Logger.getLogger(CommonSMSService.class);
	private static String sms;
	private static boolean smsOk = true;
	private static boolean jianZhouOk = false;
	private static boolean luoSiMaoOk = false;

	public void init() {
		String suffix = " is not configed!!!";
		Object smsObject = BasePropertyConfigurer.getContextProperty("sms");
		if (smsObject == null || StringUtils.isBlank(smsObject.toString())) {
			logger.error("sms" + suffix);
		} else {
			sms = smsObject.toString();
			if (sms.equals("jianzhou")) {
				Object urlObject = BasePropertyConfigurer
						.getContextProperty("sms.jianzhou.url");
				Object accountObject = BasePropertyConfigurer
						.getContextProperty("sms.jianzhou.account");
				Object passwordObject = BasePropertyConfigurer
						.getContextProperty("sms.jianzhou.password");
				Object signObject = BasePropertyConfigurer
						.getContextProperty("sms.jianzhou.sign");
				if (urlObject == null
						|| StringUtils.isBlank(urlObject.toString())) {
					logger.error("sms.jianzhou.url" + suffix);
				} else if (accountObject == null
						|| StringUtils.isBlank(accountObject.toString())) {
					logger.error("sms.jianzhou.account" + suffix);
				} else if (passwordObject == null
						|| StringUtils.isBlank(passwordObject.toString())) {
					logger.error("sms.jianzhou.password" + suffix);
				} else if (signObject == null
						|| StringUtils.isBlank(signObject.toString())) {
					logger.error("sms.jianzhou.sign" + suffix);
				} else {
					JianZhouSMS.url = urlObject.toString();
					JianZhouSMS.acccount = accountObject.toString();
					JianZhouSMS.password = passwordObject.toString();
					JianZhouSMS.sign = signObject.toString();
					jianZhouOk = true;
					logger.info("sms-jianzhou is configed successfully,url="
							+ urlObject + ",account=" + accountObject
							+ ",password=" + passwordObject + ",sign="
							+ signObject);
				}
			} else if (sms.equals("luosimao")) {
				Object urlObject = BasePropertyConfigurer
						.getContextProperty("sms.luosimao.url");
				Object keyObject = BasePropertyConfigurer
						.getContextProperty("sms.luosimao.key");
				Object signObject = BasePropertyConfigurer
						.getContextProperty("sms.luosimao.sign");
				if (urlObject == null
						|| StringUtils.isBlank(urlObject.toString())) {
					logger.error("sms.luosimao.url" + suffix);
				} else if (keyObject == null
						|| StringUtils.isBlank(keyObject.toString())) {
					logger.error("sms.luosimao.key" + suffix);
				} else if (signObject == null
						|| StringUtils.isBlank(signObject.toString())) {
					logger.error("sms.luosimao.sign" + suffix);
				} else {
					LuoSiMaoSMS.URL = urlObject.toString();
					LuoSiMaoSMS.KEY = keyObject.toString();
					LuoSiMaoSMS.SIGN = signObject.toString();
					luoSiMaoOk = true;
					logger.info("sms-luosimao is configed successfully,url="
							+ urlObject + ",KEY=" + keyObject + ",sign="
							+ signObject);
				}
			}
		}
	}

	public static void sendSMS(String mobiles, String msgContent) {
		System.out.println("mobile:"+msgContent);
		logger.info("sms send,mobile="+mobiles+"content="+msgContent);
		if (jianZhouOk) {
			//JianZhouSMS.jianZhouSMS(mobiles, msgContent);
		} else if (luoSiMaoOk) {
			try {
				mobiles = mobiles.trim();
				mobiles=mobiles.replace("，", ",");
				if (mobiles.contains(",")) {
					String[] mobileArray=mobiles.split(",");
					for(String mobile:mobileArray){
						LuoSiMaoSMS.sendSms(mobile, msgContent);
					}
				} else {
					LuoSiMaoSMS.sendSms(mobiles, msgContent);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
